package com.leyou.item.web;

import com.leyou.common.dto.PageDTO;
import com.leyou.item.dto.SkuDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.dto.SpuDTO;
import com.leyou.item.dto.SpuDetailDTO;
import com.leyou.item.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    GoodsService goodsService;

    @GetMapping("/spu/page")
    public ResponseEntity<PageDTO<SpuDTO>> listSpuByPage(
            @RequestParam(value = "brandId", required = false) Long bid,
            @RequestParam(value = "categoryId", required = false) Long cid,
            @RequestParam(value = "id", required = false) Long spuid,
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "rows", defaultValue = "5") Integer rows,
            @RequestParam(value = "saleable", required = false) Boolean isSaleable
    ) {
        return ResponseEntity.ok(goodsService.listSpuByPage(bid, cid, spuid, page, rows, isSaleable));
    }

    @PostMapping("/spu")
    public ResponseEntity<Void> addGoods(@RequestBody SpuDTO spuDTO) {
        goodsService.addSpu(spuDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @GetMapping("/sku/list")
    public ResponseEntity<List<SkuDTO>> querySkuListByIds(@RequestParam("ids") List<Long> ids) {
        return ResponseEntity.ok(goodsService.querySkuListByIds(ids));
    }

    @GetMapping("/sku/of/spu")
    public ResponseEntity<List<SkuDTO>> querySkuListBySpuId(@RequestParam("id") Long spuId) {
        return ResponseEntity.ok(goodsService.querySkuListBySpuId(spuId));
    }

    @GetMapping("/spu/detail")
    public ResponseEntity<SpuDetailDTO> querySpuDetailBySpuId(@RequestParam("id") Long spuId) {
        return ResponseEntity.ok(goodsService.querySpuDetailBySpuId(spuId));
    }

    @GetMapping("/{id}")
    public ResponseEntity<SpuDTO> getSpuById(@PathVariable("id") Long id) {
        return ResponseEntity.ok(goodsService.getSpuById(id));
    }

    @GetMapping("/spu/{id}")
    public ResponseEntity<SpuDTO> getSpuOnlyById(@PathVariable("id") Long id) {
        return ResponseEntity.ok(goodsService.getSpuOnlyById(id));
    }

    @PutMapping("/saleable")
    public ResponseEntity<Void> updateSaleable(
            @RequestParam(value = "id", required = false) Long id,
            @RequestParam(value = "saleable", required = false) Boolean saleable
    ) {
        goodsService.updateSaleable(id,saleable);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    @PutMapping("/spu")
    public ResponseEntity<Void> updateSpu(@RequestBody SpuDTO spuDTO) {
        goodsService.updateSpu(spuDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    @GetMapping("/spec/value")
    public ResponseEntity<List<SpecParamDTO>> listSpecParamBySpuId(
            @RequestParam("id") Long id,
            @RequestParam(value = "searching",required = false) Boolean isSearch
    ){
        return ResponseEntity.ok(goodsService.listSpecParamBySpuId(id,isSearch));
    }
}